package com.zf.shuashua.advert;

import android.app.Activity;
import android.text.TextUtils;

import com.zf.shuashua.MainActivity;
import com.zf.shuashua.ShuaApplication;
import com.zf.shuashua.bean.advert.AdvertConfigBean;
import com.zf.shuashua.bean.advert.AdvertConfigList;
import com.zf.shuashua.bean.advert.RewardConfigBean;
import com.zf.shuashua.utils.ManufacturerInfoUtils;
import com.zf.shuashua.utils.TimeUtils;

import java.util.Date;

/**
 * 视频视频管理类
 * 包括穿山甲、百青藤、优量汇；以及平台之前的互相补量
 */
public class RewardAdvertManager {

    private static RewardAdvertManager instance;

    private RewardAdvertManager() {
    }

    public static RewardAdvertManager getInstance() {
        if (null == instance) {
            synchronized (RewardAdvertManager.class) {
                if (null == instance) {
                    instance = new RewardAdvertManager();
                }
            }
        }
        return instance;
    }

//    public void showButtleAdvert(Activity activity, String advertType, OnRewardListener listener) {
//        if(isFullVideo()){
//            FullAdvertByteManager.getInstance().showAdvert(activity, advertType, listener);
//            return;
//        }
//        int buttleType = ShuaApplication.buttle_type;
//        if(buttleType == ButtleAdvertConstants.TYPE_BYTE_FULL){
//            //上次使用穿山甲全屏视频，本次使用穿山甲激励视频
//            ShuaApplication.buttle_type =  ButtleAdvertConstants.TYPE_BYTE_REWARD;
//            showByteAdvert(activity, advertType, listener);
//        }else if(buttleType == ButtleAdvertConstants.TYPE_BYTE_REWARD){
//            //上次使用穿山甲激励视频，本次使用优量汇激励视频
//            ShuaApplication.buttle_type =  ButtleAdvertConstants.TYPE_TENCENT_REWARD;
//            showTencentAdvert(activity, advertType, listener);
//        }else{
//            //优先全屏视频
//            ShuaApplication.buttle_type =  ButtleAdvertConstants.TYPE_BYTE_FULL;
//            showByteFullAdvert(activity, advertType, listener);
//        }
//    }

    private int count = 0;
    //小米广告间隔2个再出
    private boolean isFillCount(){
        if(count % 3 == 1){
            return true;
        }
        return false;
    }

    private final static long CLICK_INTERVAL = 500;
    private static long mLastClickTime = 0;
    protected static  boolean isFastClick() {
        if (System.currentTimeMillis() - mLastClickTime < CLICK_INTERVAL) {
            return true;
        }
        mLastClickTime = System.currentTimeMillis();
        return false;
    }

    public void showAdvert(Activity activity, String advertType, String reportId, OnRewardListener listener) {
        //广告入口防止快速点击
        if (isFastClick() ) {
            return;
        }
        count ++;
        if(ManufacturerInfoUtils.isXiaomi() && isFillCount()){
            showMimoByConfig(activity, advertType, reportId, listener);
            return;
        }
        if(ShuaApplication.isHwAdOpen() && ManufacturerInfoUtils.isHuawei() && isFillCount()){
            showHwByConfig(activity, advertType, reportId, listener);
            return;
        }
        if(CommitUtils.isFullVideo()){
            FullAdvertByteManager.getInstance().showAdvert(activity, advertType, reportId, listener);
            return;
        }
        AdvertConfigList config = ShuaApplication.advertConfig;
        if(config == null){
            showByteAdvert(activity, advertType, reportId, listener);
            return;
        }
        RewardConfigBean rewardConfig = config.getReword();
        if(rewardConfig == null){
            showByteAdvert(activity, advertType, reportId, listener);
            return;
        }
        AdvertConfigBean configBean = rewardConfig.getConfigByType(advertType);
        if(configBean == null){
            showByteAdvert(activity, advertType, reportId, listener);
            return;
        }
        String codeId = configBean.getOrigin_id();
        //芒果广告
        if(configBean.isMg()){
            showMgByConfig(activity, advertType, reportId, listener);
            return;
        }
        //艾狄墨搏AdMobile
        if(configBean.isAdMobile()){
            showAdMobile(activity, advertType, reportId, codeId, listener);
            return;
        }
        //旺翔
        if(configBean.isWangXiang()){
            showWxByConfig(activity, advertType, reportId, listener);
            return;
        }
        //自身广告
        if(configBean.isSelf()){
            RewardAdvertFragment advertFragment = RewardAdvertFragment.newInstance(advertType, reportId, codeId);
            advertFragment.setOnRewardListener(listener);
            if (activity instanceof MainActivity) {
                ((MainActivity) activity).showFragment(android.R.id.content, advertFragment, true);
            }else if (activity instanceof com.zf.shuashua.ui.gift.H5Activity) {
                ((com.zf.shuashua.ui.gift.H5Activity) activity).showFragment(android.R.id.content, advertFragment, true);
            }
            return;
        }
        if(configBean.isFull()){
            showByteFullByConfig(activity, advertType, reportId, codeId, listener);
            return;
        }
        if(configBean.isTencent()){
            showTencentByConfig(activity, advertType, reportId, codeId, listener);
        }else{
            showByteByConfig(activity, advertType, reportId, codeId, listener);
        }
    }

    public void showAdvert(Activity activity, String advertType, OnRewardListener listener) {
        showAdvert(activity, advertType, "", listener);
    }

    /**
     *  请求穿山甲全屏视频，无广告时，用穿山甲激励视频
     * @param activity
     * @param advertType
     * @param listener
     */
    private void showByteFullByConfig(Activity activity, String advertType, String reportId, String codeId, OnRewardListener listener) {
        FullAdvertByteManager.getInstance().showAdvert(activity, advertType, reportId, codeId, new OnRewardListener() {
            @Override
            public void onAdClose() {
                if(listener != null){
                    listener.onAdClose();
                }
            }

            @Override
            public void onAdClick() {
                if(listener != null){
                    listener.onAdClick();
                }
            }

            @Override
            public void onVideoComplete() {
                if(listener != null){
                    listener.onVideoComplete();
                }
            }

            @Override
            public void onVideoError() {
                //穿山甲全屏视频，补穿山甲激励视频
                RewardAdvertByteManager.getInstance().showAdvert(activity, advertType, reportId, listener);
            }
        });
    }

    /**
     *  请求穿山甲全屏视频，无广告时，用穿山甲激励视频
     * @param activity
     * @param advertType
     * @param listener
     */
    private void showByteFullAdvert(Activity activity, String advertType, String reportId, OnRewardListener listener) {
        FullAdvertByteManager.getInstance().showAdvert(activity, advertType, reportId, new OnRewardListener() {
            @Override
            public void onAdClose() {
                if(listener != null){
                    listener.onAdClose();
                }
            }

            @Override
            public void onAdClick() {
                if(listener != null){
                    listener.onAdClick();
                }
            }

            @Override
            public void onVideoComplete() {
                if(listener != null){
                    listener.onVideoComplete();
                }
            }

            @Override
            public void onVideoError() {
                //穿山甲全屏视频，补穿山甲激励视频
                RewardAdvertByteManager.getInstance().showAdvert(activity, advertType, reportId, listener);
            }
        });
    }

    /**
     *  请求穿山甲广告，无广告时，用优量汇补位
     * @param activity
     * @param advertType
     * @param listener
     */
    private void showByteAdvert(Activity activity, String advertType, String reportId, OnRewardListener listener) {
        RewardAdvertByteManager.getInstance().showAdvert(activity, advertType, reportId, new OnRewardListener() {
            @Override
            public void onAdClose() {
                if(listener != null){
                    listener.onAdClose();
                }
            }

            @Override
            public void onAdClick() {
                if(listener != null){
                    listener.onAdClick();
                }
            }

            @Override
            public void onVideoComplete() {
                if(listener != null){
                    listener.onVideoComplete();
                }
            }

            @Override
            public void onVideoError() {
                //穿山甲广告加载失败，补位优量汇广告
                RewardAdvertTencentManager.getInstance().showAdvert(activity, advertType, reportId, listener);
            }
        });
    }

    /**
     *  请求优量汇广告，无广告时，用穿山甲补位
     * @param activity
     * @param advertType
     * @param listener
     */
    private void showTencentAdvert(Activity activity, String advertType, String reportId, OnRewardListener listener) {
        RewardAdvertTencentManager.getInstance().showAdvert(activity, advertType, reportId, new OnRewardListener() {
            @Override
            public void onAdClose() {
                if(listener != null){
                    listener.onAdClose();
                }
            }

            @Override
            public void onAdClick() {
                if(listener != null){
                    listener.onAdClick();
                }
            }

            @Override
            public void onVideoComplete() {
                if(listener != null){
                    listener.onVideoComplete();
                }
            }

            @Override
            public void onVideoError() {
                //优量汇广告加载失败，补位穿山甲广告
                RewardAdvertByteManager.getInstance().showAdvert(activity, advertType, reportId, listener);
            }
        });
    }

    /**
     *  请求穿山甲广告，无广告时，用优量汇补位
     * @param activity
     * @param advertType
     * @param listener
     */
    private void showByteByConfig(Activity activity, String advertType, String reportId, String codeId, OnRewardListener listener) {
        RewardAdvertByteManager.getInstance().showAdvert(activity, advertType, reportId, codeId, new OnRewardListener() {
            @Override
            public void onAdClose() {
                if(listener != null){
                    listener.onAdClose();
                }
            }

            @Override
            public void onAdClick() {
                if(listener != null){
                    listener.onAdClick();
                }
            }

            @Override
            public void onVideoComplete() {
                if(listener != null){
                    listener.onVideoComplete();
                }
            }

            @Override
            public void onVideoError() {
                //穿山甲广告加载失败，补位优量汇广告
                RewardAdvertTencentManager.getInstance().showAdvert(activity, advertType, reportId, listener);
//                RewardAdvertByteManager.getInstance().showAdvert(activity, advertType, listener);
            }
        });
    }

    /**
     *  请求优量汇广告，无广告时，用穿山甲补位
     * @param activity
     * @param advertType
     * @param listener
     */
    private void showTencentByConfig(Activity activity, String advertType, String reportId, String codeId, OnRewardListener listener) {
        RewardAdvertTencentManager.getInstance().showAdvert(activity, advertType, reportId, codeId, new OnRewardListener() {
            @Override
            public void onAdClose() {
                if(listener != null){
                    listener.onAdClose();
                }
            }

            @Override
            public void onAdClick() {
                if(listener != null){
                    listener.onAdClick();
                }
            }

            @Override
            public void onVideoComplete() {
                if(listener != null){
                    listener.onVideoComplete();
                }
            }

            @Override
            public void onVideoError() {
                RewardAdvertByteManager.getInstance().showAdvert(activity, advertType, reportId, listener);
            }
        });
    }

    /**
     * 芒果广告
     * @param activity
     * @param advertType
     * @param listener
     */
    private void showMgByConfig(Activity activity, String advertType, String reportId, OnRewardListener listener){
        RewardAdvertMgManager.getInstance().showAdvert(activity, advertType, reportId, new OnRewardListener() {
            @Override
            public void onAdClose() {
                if(listener != null){
                    listener.onAdClose();
                }
            }

            @Override
            public void onAdClick() {
                if(listener != null){
                    listener.onAdClick();
                }
            }

            @Override
            public void onVideoComplete() {
                if(listener != null){
                    listener.onVideoComplete();
                }
            }

            @Override
            public void onVideoError() {
                //芒果广告加载失败，补位穿山甲广告
                RewardAdvertByteManager.getInstance().showAdvert(activity, advertType, reportId, listener);
            }
        });
    }

    /**
     * 华为广告
     */
    private void showHwByConfig(Activity activity, String advertType, String reportId, OnRewardListener listener) {
        RewardAdvertHwManager.getInstance().showAdvert(activity, advertType, reportId, new OnRewardListener() {
            @Override
            public void onAdClose() {
                if (listener != null) {
                    listener.onAdClose();
                }
            }

            @Override
            public void onAdClick() {
                if (listener != null) {
                    listener.onAdClick();
                }
            }

            @Override
            public void onVideoComplete() {
                if (listener != null) {
                    listener.onVideoComplete();
                }
            }

            @Override
            public void onVideoError() {
                //华为广告加载失败，补位穿山甲广告
                RewardAdvertByteManager.getInstance().showAdvert(activity, advertType, reportId, listener);
            }
        });
    }

    /**
     * 米盟广告
     */
    private void showMimoByConfig(Activity activity, String advertType, String reportId, OnRewardListener listener) {
        RewardAdvertMimoManager.getInstance().showAdvert(activity, advertType, reportId, new OnRewardListener() {
            @Override
            public void onAdClose() {
                if (listener != null) {
                    listener.onAdClose();
                }
            }

            @Override
            public void onAdClick() {
                if (listener != null) {
                    listener.onAdClick();
                }
            }

            @Override
            public void onVideoComplete() {
                if (listener != null) {
                    listener.onVideoComplete();
                }
            }

            @Override
            public void onVideoError() {
                //米盟广告加载失败，补位穿山甲广告
                RewardAdvertByteManager.getInstance().showAdvert(activity, advertType, reportId, listener);
            }
        });
    }

    /**
     * 旺翔广告
     */
    private void showWxByConfig(Activity activity, String advertType, String reportId, OnRewardListener listener) {
        RewardAdvertWxManager.getInstance().showAdvert(activity, advertType, reportId, new OnRewardListener() {
            @Override
            public void onAdClose() {
                if (listener != null) {
                    listener.onAdClose();
                }
            }

            @Override
            public void onAdClick() {
                if (listener != null) {
                    listener.onAdClick();
                }
            }

            @Override
            public void onVideoComplete() {
                if (listener != null) {
                    listener.onVideoComplete();
                }
            }

            @Override
            public void onVideoError() {
                //旺翔广告加载失败，补位穿山甲广告
                RewardAdvertByteManager.getInstance().showAdvert(activity, advertType, reportId, listener);
            }
        });
    }

    /**
     * adMobile广告
     */
    private void showAdMobile(Activity activity, String advertType, String reportId, String codeId, OnRewardListener listener) {
        RewardAdvertMobileManager.getInstance().showAdvert(activity, advertType, reportId, codeId, new OnRewardListener() {
            @Override
            public void onAdClose() {
                if (listener != null) {
                    listener.onAdClose();
                }
            }

            @Override
            public void onAdClick() {
                if (listener != null) {
                    listener.onAdClick();
                }
            }

            @Override
            public void onVideoComplete() {
                if (listener != null) {
                    listener.onVideoComplete();
                }
            }

            @Override
            public void onVideoError() {
                //AdMoble广告加载失败，补位穿山甲广告
                RewardAdvertByteManager.getInstance().showAdvert(activity, advertType, reportId, listener);
            }
        });
    }


}
